package rsi.mesoscopic.ui;

import java.awt.BorderLayout;
import java.io.File;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

import rsi.mesoscopic.model.NetworkModel;
import rsi.mesoscopic.model.XMLMesoscopicReader;

public class MainFrame extends JFrame {

	private static final long serialVersionUID = 1L;

	private MesoscopicSimulationPanel mMesoscopicPanel;

	private final NetworkModel mModel;

	public MainFrame() {

	    JFileChooser jfc = new JFileChooser();

	    if ( jfc.showDialog(this, "Open") == JFileChooser.CANCEL_OPTION) System.exit(0);

		File test = jfc.getSelectedFile();
		XMLMesoscopicReader reader = new XMLMesoscopicReader();
		SAXParserFactory factory = SAXParserFactory.newInstance();
		try {
			SAXParser parser = factory.newSAXParser();

			parser.parse(test, reader);
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		mModel = reader.getNetworkModel();
		mModel.setStep(0);

		initLayout();
		initToolbar();

	}

	protected void initLayout() {
		mMesoscopicPanel = new MesoscopicSimulationPanel(mModel);
		//mMesoscopicPanel.setPreferredSize(new Dimension(500,500));

		setLayout(new BorderLayout());
		add(mMesoscopicPanel, BorderLayout.CENTER);

		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(400,400);
		setVisible(true);
	}

	protected void initToolbar() {

	}

}
